#!/bin/bash
# Program:
#  为骑马与砍杀 mod 选择提供界面
# History:
# 2019-02-20 zerolee First release

# 使用：
#      将 ~/公共/game/mbwarband_beta_amd64.appimage 修改为自己的
#      mbwarband_beta_amd64.appimage 存放路径
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 备份存档
function savegame_backup() {
    while true
    do
	[ -e /tmp/"$1""_old" ] && rm -rf /tmp/"$1""_old"
	[ -e /tmp/"$1" ] && mv -f /tmp/"$1" /tmp/"$1""_old"
	[ -e ~/.mbwarband/Savegames/"$1" ] && cp -rf ~/.mbwarband/Savegames/"$1" /tmp/
	sleep 10m
    done
}

# 设置游戏语言为中文
[ -e ~/.mbwarband/language.txt ] || echo "cns" > ~/.mbwarband/language.txt

# 初始化相关变量
i=3
mods=(zhantuan napoleonic viking)
modi=(FALSE FALSE FALSE)
modls=`ls -F ~/.mbwarband/|grep \.\*/|egrep -v "(SceneObj|mod|Savegames|Characters/$)"`
OLDIFS=$IFS
IFS=$'\n'

for var in $modls
do
    mods[$i]="${var%*/}"
    [ -d ~/.mbwarband/Savegames/"${var%*/}" ] || mkdir  ~/.mbwarband/Savegames/"${var%*/}"
    modi[$i]=FALSE
    i=$[$i + 1]
done

# 计算出 mod 中最长文件名的长度
max=${#mods[0]}
for (( m=1; m<i; m=m+1 ))
do
    if [ $max -lt ${#mods[$m]} ]; then
	max=${#mods[$m]}
    fi
done

# 清除已经删除 mod 的存档
modsave=`ls -F ~/.mbwarband/Savegames/|egrep -v "(Native|mod|Napoleonic|Viking)"`
for var in $modsave
do
    for (( l=3; l<i; l=l+1 ))
    do
	if [ "${var%*/}" == "${mods[$l]}" ]; then
	    break
	fi
    done
    if [ "$l" == "$i" ]; then
	rm -rf ~/.mbwarband/Savegames/"$var"
    fi
done

# 首先判断 last_module_warband 存在不存在，
# 存在则启动此文件中记录的 mod 为默认, 否则战团为默认
if [ -f ~/.mbwarband/last_module_warband ]; then
    lmw=`cat ~/.mbwarband/last_module_warband`
    if [ "$lmw" == "mod" ]; then
        mod=`file ~/.mbwarband/mod`
	if [ -z "${mod##*broken*}" ]; then
	    rm ~/.mbwarband/last_module_warband
	    rm -rf ~/.mbwarband/mod
	    rm -rf ~/.mbwarband/Savegames/mod
	    rm -rf ~/.mbwarband/Savegames/"${mod##*/}"
	    modi[0]=TRUE
	else
            k=3
            for var in $modls
            do
		if [ "${mod##*/}" == "${var%*/}" ]; then
                    modi[$k]=TRUE
		fi
		k=$[$k + 1]
            done
	fi
    elif [ "$lmw" == "Napoleonic Wars" ]; then
        modi[1]=TRUE
    else
        modi[2]=TRUE
    fi
else
    modi[0]=TRUE
fi

# 将 modi 和 mods 进行连接， 为 zenity 的 radiolist 提供值。
for (( j=0; j<i; j=j+1 ))
do
    modz=$modz$'\n'${modi[$j]}$'\n'${mods[$j]}
done

ans=$(zenity  --list  --text "骑马与砍杀 mod 选择" --radiolist  --column "选择" \
              --column "mod" $modz --height=$[200 + 20 * $[$i - 3]] --width=$[100 + 7 * $max]);

# 用户进行选择，选择战团，则 last_module_warband 文件必须消失
if [ ! -z $ans ]; then
    [ -f ~/.mbwarband/last_module_warband ] && rm ~/.mbwarband/last_module_warband
    [ -e ~/.mbwarband/mod ] && rm -rf ~/.mbwarband/mod
    [ -e ~/.mbwarband/Savegames/mod ] && rm -rf ~/.mbwarband/Savegames/mod
    case $ans in
        "zhantuan")
	    savegame_backup "Native" &
        ;;
        "napoleonic")
            echo "Napoleonic Wars" >  ~/.mbwarband/last_module_warband
	    savegame_backup "Napoleonic Wars" &
            ;;
        "viking")
            echo "Viking Conquest" >  ~/.mbwarband/last_module_warband
	    savegame_backup "Viking Conquest" &
            ;;
        *)
            echo "mod" > ~/.mbwarband/last_module_warband
            ln -s ~/.mbwarband/"$ans" ~/.mbwarband/mod
            ln -s ~/.mbwarband/Savegames/"$ans" ~/.mbwarband/Savegames/mod
	    [ -e ~/.mbwarband/"$ans"/mod ] && rm -rf ~/.mbwarband/"$ans"/mod
	    savegame_backup "$ans" &
            ;;
    esac
    ~/公共/game/mbwarband_beta_amd64.appimage

    for p_pid in `ps -ef |grep mbwarband |egrep -v grep|awk '{print $2}'`
    do
	pkill -9 -P $p_pid
    done

fi

IFS=$OLDIFS
